package lab03.test05;

import java.io.*;

public class Test05 {
    public static void main(String[] args) {
        String path = "F:\\Test01.txt";
        File myFile = new File(path);
        try {
            myFile.createNewFile();
            System.out.println("创建文件成功.");
        } catch (IOException e) {
            e.printStackTrace();
        }                   //利用file类创建一个新的txt文件

        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(path));
            String s = "Hello";

            for(int i = 0; i < s.length(); i++) {
                writer.write((char)(s.charAt(i) + 1));
            } //循环写入并加密字符串

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(path));
            System.out.println("输出加密文件.");
            String s = reader.readLine();
            for(int i = 0; i < s.length(); i++) {
                System.out.print((char)(s.charAt(i) - 1));     //解密文件并输出到控制台
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //myFile.delete();
    }
}


